package com.lysj.bhp.manager.controller;



import com.lysj.bhp.manager.base.ManagerContent;
import com.lysj.bhp.manager.domain.Privilege;
import com.lysj.bhp.manager.domain.Staff;
import com.lysj.bhp.util.json.GsonUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.util.web.SelectItem;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("admin/staff")
public class StaffController extends ManagerContent {

    // 基本接口///////////////////////////////////////
    @PostMapping(value = "/login")
    public Resp login(Staff model) {
        System.out.println(model.getUsername());
        System.out.println(model.getPassword());
        if (ParamUtil.isBlack(model.getUsername()) || ParamUtil.isBlack(model.getPassword())) {
            return new Resp(Resp.PARAM_ERROR, "账号或者密码为空");
        }
        model.setLoginIp(request.getRemoteAddr());
        model.setLoginDate(System.currentTimeMillis());

        Staff staff = staffRepository.findByUsername(model.getUsername());
        if (staff == null || !staff.getPassword().equals(ParamUtil.md5(model.getPassword()))) {
            return new Resp(Resp.PARAM_ERROR, "账号或者密码错误");
        }
        if (staff.getExpires() < System.currentTimeMillis()) {// token不在有效期内,更新token
            staff.setToken(ParamUtil.uuid());
        }
        staff.setExpires(System.currentTimeMillis() + LOGIN_EXPIRES_MANAGER);//
        staff.setLoginIp(model.getLoginIp());
        staff.setLoginDate(model.getLoginDate());
        staffRepository.save(staff);
        return new Resp(staff);
    }

    @GetMapping(value = "/my_priv")
    public Resp myPriv() {
        List<Privilege> privileges = new ArrayList<>();
        if (currentStaff().isAdmin()) {
            privileges = privilegeRepository.findAll();
        } else {
            if (!ParamUtil.isBlack(currentStaff().getPrivJson())) {
                privileges = GsonUtil.toList(currentStaff().getPrivJson(), Privilege.class);
            }
        }
        return new Resp(privileges);
    }

    @GetMapping(value = "/isLogin")
    public Resp isLogin(String token) {
        Staff staff = staffRepository.findByToken(token);
        if (staff == null) {
            return new Resp(Resp.TOKEN_ERROR, "登录信息已经失效,请重新登录");
        }
        if (staff.getExpires() < System.currentTimeMillis()) {
            return new Resp(Resp.TOKEN_ERROR, "登录信息已经失效,请重新登录");
        }
        staff.setExpires(System.currentTimeMillis() + LOGIN_EXPIRES_MANAGER);
        staffRepository.save(staff);
        return new Resp(staff);
    }

    @GetMapping(value = "/quit")
    public Resp quit(String token) {
        Staff staff = staffRepository.findByToken(token);
        if (staff != null) {
            staff.setExpires(System.currentTimeMillis());
        }
        return new Resp("退出成功!");
    }

    @GetMapping("selectItem")
    public Resp selectItem() {
        List<SelectItem> selectItemList = roleRepository.selectItems();
        return new Resp(selectItemList);
    }
}
